<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 4:39
 */
namespace Model;
class ActivityMemberTreasureMod extends Mod
{
	public function __construct()
	{
		$this->_table = 'kuyuan_vbz_activity_member_treasure';
	}

	/**
	 * 实物列表
	 * @param $_activity_id
	 * @param $_status
	 * @param $_limit
	 * @param $_uid
	 */
	public function entityList($_activity_id,$_status,$_limit,$_uid = 0)
	{
		$_where = "activity_id = :activity_id
						AND
					exchange = :exchange
						AND
					type = :type";
		$_bind = array('activity_id'=>$_activity_id,'exchange'=>$_status,'type'=>2);
		if(!empty($_uid)){
			$_where .= " AND uid = :uid ";
			$_bind['uid'] = $_uid;
		}
		$_sql = "
					SELECT
							uid,exchange
						FROM
							".tablename($this->_table)."
						WHERE
							$_where
						GROUP BY
							uid
						ORDER BY
							uid ASC
						LIMIT
							{$_limit[0]},{$_limit[1]}	
				";
		return pdo_fetchall($_sql,$_bind);
	}

	/**实物列表总数
	 * @param $_activity_id
	 * @param $_status
	 * @param $_uid
	 */
	public function entityListTotal($_activity_id,$_status,$_uid = 0)
	{
		$_where = "activity_id = :activity_id
						AND
					exchange = :exchange
						AND
					type = :type";
		$_bind = array('activity_id'=>$_activity_id,'exchange'=>$_status,'type'=>2);
		if(!empty($_uid)){
			$_where .= " AND uid = :uid";
			$_bind['uid'] = $_uid;
		}
		$_sql = "
					SELECT
							COUNT(distinct uid) as count
						FROM
							".tablename($this->_table)."
						WHERE
							$_where
				";
		$_data = pdo_fetchcolumn($_sql,array('activity_id'=>$_activity_id,'exchange'=>$_status,'type'=>2));
		return $_data;
	}

	/**实物列表总数
	 * @param $_activity_id
	 * @param $_status
	 * @param $_uid
	 */
	public function joinMemberTotal($_activity_id)
	{
		$_sql = "
					SELECT
							COUNT(distinct uid) as count
						FROM
							".tablename($this->_table)."
						WHERE
							activity_id = :activity_id
				";
		$_data = pdo_fetchcolumn($_sql,array('activity_id'=>$_activity_id));
		return $_data;
	}

    /**
     * 获取卡券列表
     * @param uid 用户id
     * @param activity_id 活动id
     * @param $_type 卡券类型
     * @param $_limit 请求数量和下标
     * @return 返回数据
     */
	public function getCouponList($_uid,$_activity_id = 0,$_type,Array $_limit)
    {
        $_where = "
                        A.uid = :uid
                    AND
                        A.type = 3
                   ";
        $_bind = array('uid'=>$_uid);
        if($_activity_id != 0){
            $_where .= " AND A.activity_id = :activity_id";
            $_bind['activity_id'] = $_activity_id;
        }
        if($_type != 'qb'){
            $_where .= " AND C.type = :type ";
            $_bind['type'] = $_type;
        }
        $_sql_limit = '';
        if(count($_limit) > 0){
            $_sql_limit = "LIMIT {$_limit[0]},{$_limit[1]}";
        }
        $_sql = "SELECT
                        A.id,A.number,A.exchange,A.expires,C.name,C.cost,C.type,C.store_id,B.use_limit_type
                 FROM
                        ".tablename($this->_table)." as A
                 LEFT JOIN
                        ".tablename('kuyuan_vbz_activity_coupon')." as B
                 ON
                        A.treasure_id = B.id
                 RIGHT JOIN
                        ".tablename('kuyuan_vbz_store_coupon')." as C
                 ON
                        B.coupon_id = C.id
                 WHERE
                        ".$_where."
                 GROUP BY
                        A.treasure_id
                 ORDER BY
                        A.exchange = A.number ASC,A.id DESC
                 ".$_sql_limit."
                ";
        return pdo_fetchall($_sql,$_bind);
    }

}